package com.oa.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.oa.back.ResponseBack;
import com.oa.common.enums.CodeStatus;
import com.oa.common.vo.Propal;
import com.oa.entities.User;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @name 用户的拦截类
 * @author Yang
 */
public class UserInterceptor extends HandlerInterceptorAdapter {

    /** 重定向视图名称前缀 */
    private static final String REDIRECT_VIEW_NAME_PREFIX = "redirect:";

    /** "重定向URL"参数名称 */
    private static final String REDIRECT_URL_PARAMETER_NAME = "redirectUrl";

    /** "管理员"属性名称 */
    private static final String ADMIN_ATTRIBUTE_NAME = "admin";

    /** 默认登录URL */
    private static final String DEFAULT_LOGIN_URL = "/login/index.html";

    /** 登录URL */
    private String loginUrl = DEFAULT_LOGIN_URL;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Propal principal = (Propal) session.getAttribute(User.USER_COOKIE_NAME);
        if (principal != null && request.getLocalAddr().equals(principal.getIp())) {
            return true;
        }
        String requestType = request.getHeader("X-Requested-With");
        if (requestType != null && requestType.equalsIgnoreCase("XMLHttpRequest")) {
            response.addHeader("loginStatus", "accessDenied");
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return false;
        }
        if (request.getMethod().equalsIgnoreCase("GET")) {
            String redirectUrl = request.getQueryString() != null ? request.getRequestURI() + "?" + request.getQueryString() : request.getRequestURI();
            response.sendRedirect(request.getContextPath() + loginUrl + "?" + REDIRECT_URL_PARAMETER_NAME + "=" + URLEncoder.encode(redirectUrl, "utf-8"));
        } else {
            ResponseBack<String> responseBack = ResponseBack.fial(CodeStatus.USER_LOGIN_DUE.getResponseStatus());
            response.setContentType("application/json; charset=utf-8");
            OutputStream stream = response.getOutputStream();
            String value = JSONObject.toJSONString(responseBack);
            stream.write(value.getBytes("utf-8"));
            stream.flush();
            stream.close();
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HttpSession session = request.getSession();
        Propal principal = (Propal) session.getAttribute(User.USER_COOKIE_NAME);
        if (principal != null && request.getLocalAddr().equals(principal.getIp())) {
            return;
        }


    }

    public String getLoginUrl() {
        return loginUrl;
    }

    public void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }
}
